;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Stock.BackOut", [])
        .controller("BackOutCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", "EventBus", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService, EventBus) {
            var btnPermissionCode = {
                backOutAdd: window.ActionCode.backOutAdd,
                backOutEdit: window.ActionCode.backOutEdit,
                backOutDel: window.ActionCode.backOutDel,
                backOutExport: window.ActionCode.backOutExport,
                backOutPrint: window.ActionCode.backOutPrint,
                backOutFinish: window.ActionCode.backOutFinish,
                backOutOut: window.ActionCode.backOutOut
            };
            var sortParam = "";
            var sortFiled = {};

            $scope.activeFlag = [true, false, false, false, false];
            $scope.datePickerFlag = [false, false];
            $scope.btnPermission = {
                backOutAdd: true,
                backOutEdit: true,
                backOutPrint: true,
                backOutDel: true,
                backOutFinish: true,
                backOutExport: true,
                backOutOut: true
            };
            $scope.submitted = false;
            $scope.markInfo = {
                allChecked: false,
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.billPageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.condition = {};
            $scope.orderCondition = {};
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };
            $scope.dataList = [];
            $scope.purchaseBillList = [];
            $scope.detailInfo = {
                headerInfo: {},
                materialList: []
            };
            $scope.steps = {
                curStep: 0,
                list: [{name: '选择回购订单'}, {name: '生成回购出库单'}]
            };
            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/backOutbound/export"),
                sectionParam: {ids: []},
                allUrl: ybHttp.RequestUrl("device", "/backOutbound/exportAll")
            };
            $scope.warehouseList = [];
            $scope.tableConfig = {
                fixedWidth: 230,
                scrollWidth: 1090,
                tableCol: [
                    {
                        type: "",
                        name: "回购出库单号",
                        dataField: "outboundOrder",
                        width: 170,
                        isShow: true,
                        isFixed: false,
                        clickFn: "viewDetail",
                        toSort: true,
                        sortField: "",
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "回购订单号",
                        dataField: "backOrder",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortField: "",
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "调出仓库",
                        dataField: "warehouseCode",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortField: "",
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "调出库区",
                        dataField: "inArea",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortField: "",
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "出库状态",
                        dataField: "statusShow",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortField: "",
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "制单人",
                        dataField: "createName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "制单时间",
                        dataField: "createDateShow",
                        width: 130,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortField: "",
                        sortType: ""
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 250,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };
            $scope.purchaseBillTbConf = {
                scrollWidth: 830,
                tableCol: [
                    {
                        type: "",
                        name: "回购单号",
                        dataField: "backOrder",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "客户名称",
                        dataField: "purchaseOrgName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "调度员",
                        dataField: "dispatcherName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "出库单状态",
                        dataField: "createOutStatusDes",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "出库状态",
                        dataField: "outStatusDes",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "创建时间",
                        dataField: "createDate",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 80,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };
            $scope.newTbConf = {
                scrollWidth: 1000,
                tableCol: [
                    {
                        name: "零件编号",
                        width: 150,
                        dataField: "materialsNo",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "零件中文名",
                        width: 150,
                        dataField: "materialsDes",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "零件类型",
                        width: 150,
                        dataField: "materialsType",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "单位",
                        width: 100,
                        dataField: "unit",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "单价",
                        width: 100,
                        dataField: "price",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "订单总数",
                        width: 100,
                        dataField: "qty",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "已发数量",
                        width: 100,
                        dataField: "sendQty",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "可出库数量",
                        width: 100,
                        dataField: "canInQty",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "本次出库数量",
                        width: 120,
                        dataField: "amount",
                        input: true,
                        inputType: "num"
                    }
                ]
            };
            $scope.editTbConf = {
                scrollWidth: 1120,
                tableCol: [
                    {
                        name: "零件编号",
                        width: 150,
                        dataField: "materialsNo",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "零件中文名",
                        width: 150,
                        dataField: "materialsDes",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "零件类型",
                        width: 150,
                        dataField: "materialsType",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "单位",
                        width: 100,
                        dataField: "unit",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "订单总数",
                        width: 100,
                        dataField: "qty",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "已发数量",
                        width: 100,
                        dataField: "sendQty",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "可出库数量",
                        width: 100,
                        dataField: "canInQty",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "本次出库数量",
                        width: 120,
                        dataField: "amount",
                        input: true,
                        inputType: "num"
                    },
                    {
                        name: "实际出库数量",
                        width: 120,
                        dataField: "realAmount",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "出库状态",
                        width: 100,
                        dataField: "statusShow",
                        input: false,
                        inputType: ""
                    }
                ]
            };
            $scope.detailTbConf = {
                scrollWidth: 1080,
                tableCol: [
                    {
                        name: "零件编号",
                        width: 150,
                        dataField: "materialsNo",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "零件中文名",
                        width: 150,
                        dataField: "materialsDes",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "零件类型",
                        width: 150,
                        dataField: "materialsType",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "单位",
                        width: 100,
                        dataField: "unit",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "订单总数",
                        width: 100,
                        dataField: "qty",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "已发数量",
                        width: 100,
                        dataField: "sendQty",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "本次出库数量",
                        width: 120,
                        dataField: "amount",
                        input: true,
                        inputType: "num"
                    },
                    {
                        name: "实际出库数量",
                        width: 120,
                        dataField: "realAmount",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "出库状态",
                        width: 100,
                        dataField: "statusShow",
                        input: false,
                        inputType: ""
                    }
                ]
            };
            $scope.inTbConf = {
                scrollWidth: 1160,
                tableCol: [
                    {
                        name: "零件编号",
                        width: 150,
                        dataField: "materialsNo",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "零件中文名",
                        width: 150,
                        dataField: "materialsDes",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "零件类型",
                        width: 150,
                        dataField: "materialsType",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "单位",
                        width: 100,
                        dataField: "unit",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "订单总数",
                        width: 100,
                        dataField: "qty",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "已发数量",
                        width: 100,
                        dataField: "sendQty",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "出库数量",
                        width: 100,
                        dataField: "amount",
                        input: false,
                        inputType: "num"
                    },
                    {
                        name: "储位",
                        width: 150,
                        dataField: "locationCode",
                        input: true,
                        inputType: "func"
                    },
                    {
                        name: "出库状态",
                        width: 100,
                        dataField: "statusShow",
                        input: false,
                        inputType: ""
                    }
                ]
            };
            $scope.tabFlag = {};
            $scope.inState = [
                {id: 0, name: "未出库"},
                {id: 1, name: "部分出库"},
                {id: 2, name: "已出库"},
                {id: 3, name: "强制结单"}
            ];

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    billFixedWidth: {left: $scope.tableConfig.fixedWidth + "px"}
                };
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        initData();
                        break;
                    case 1:
                        if ($scope.warehouseList.length === 0) {
                            getWarehouse();
                        }
                        break;
                    case 2:
                        if ($scope.warehouseList.length === 0) {
                            getWarehouse();
                        }
                        break;
                    case 3:
                        break;
                    case 4:
                        break;
                    default:
                        break;
                }
            };

            $scope.toSort = function (config, index) {
                // return;
                if (!config[index].toSort) {
                    return;
                }
                var sortType = ybWidget.ToSort(config, index);
                sortFiled = config[index];
                sortParam = sortFiled.sortField + " " + sortType;
                getDataList(1);
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewDetail":
                        viewDetail(item);
                        break;
                    case "viewSapError":
                        viewSapError(item);
                        break;
                    default:
                        break;
                }
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.switchDataPicker = function (index) {
                $scope.datePickerFlag = [false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {seller: false};
            };

            $scope.selectSingle = function (item) {
                if (!item.isChecked) {
                    $scope.markInfo.allChecked = false;
                } else {
                    if (!checkAll()) {
                        $scope.markInfo.allChecked = false;
                    } else {
                        $scope.markInfo.allChecked = true;
                    }
                }
                geBillIds();
            };

            $scope.selectAll = function () {
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    $scope.dataList[i].isChecked = $scope.markInfo.allChecked;
                }
                geBillIds();
            };

            $scope.add = function () {
                $scope.tabFlag.type = 0;
                getPurchaseBill(1);
                $scope.changeTab(1);
            };

            $scope.orderSearch = function () {
                getPurchaseBill();
            };

            $scope.selBill = function (item) {
                $scope.steps.curStep = 1;
                $scope.detailInfo.headerInfo = {};
                $scope.detailInfo.materialList = [];
                ybHttp.Get('device', '/buyBack/item', {id: item.id}).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.detailInfo.headerInfo.backOrder = resp.value.headerInfo.backOrder;
                    $scope.detailInfo.headerInfo.purchaseOrgId = resp.value.headerInfo.purchaseOrgId;
                    $scope.detailInfo.headerInfo.purchaseOrgName = resp.value.headerInfo.purchaseOrgName;
                    $scope.detailInfo.headerInfo.createId = $scope.curUser.userId;
                    $scope.detailInfo.headerInfo.createName = $scope.curUser.name;
                    $scope.detailInfo.headerInfo.createDate = +new Date();
                    $scope.detailInfo.headerInfo.warehouseCode = "";
                    $scope.detailInfo.headerInfo.note = "";
                    angular.forEach(resp.value.materialList, function (item) {
                        if(item.qty > item.createOutQty){
                            var materielItem = {};
                            materielItem.materialsNo = item.materialsNo;
                            materielItem.materialsDes = item.materialsDes;
                            materielItem.materialsType = item.materialsType;
                            materielItem.unit = item.unit;
                            materielItem.price = item.price;
                            materielItem.qty = item.qty;
                            materielItem.sendQty = !!item.createOutQty ? item.createOutQty : 0;
                            materielItem.canInQty = ybWidget.Minus(item.qty, materielItem.sendQty);
                            materielItem.amount = ybWidget.Minus(item.qty, materielItem.sendQty);
                            $scope.detailInfo.materialList.push(materielItem);
                        }
                    });

                });
            };

            $scope.delData = function (delType, billId) {
                var service = "device";
                var reqUrl = "/backOutbound/remove";
                var billIds = [];

                if (delType === "single") {
                    billIds.push(billId);
                }

                if (delType === "batch") {
                    for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                        if (!!$scope.dataList[i].isChecked) {
                            billIds.push($scope.dataList[i].id);
                        }
                    }
                }

                if (billIds.length === 0) {
                    ybWidget.ShowMessage('请选择需要删除的行');
                    return;
                }
                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function () {
                    ybHttp.Post(service, reqUrl, billIds).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    });
                });
            };

            $scope.printBill = function (item) {
                var id = item.id;
                var billIds = [];
                billIds.push(id);
                var param = {
                    billId: billIds,
                    printType: "BackOut"
                };
                ybWidget.SetIsPrintOrPreview(param);
            };

            $scope.selectWarehouse = function () {
                ybWidget.WarehouseUsingSelector().then(function (data) {
                    if (!!$scope.detailInfo.headerInfo.warehouseCode && (data.warehouseCode !== $scope.detailInfo.headerInfo.warehouseCode)) {
                        $scope.detailInfo.headerInfo.inArea = "";
                        $scope.detailInfo.headerInfo.sapInArea = "";
                    }
                    $scope.detailInfo.headerInfo.warehouseCode = data.warehouseCode;
                })
            };

            $scope.clearWarehouse = function () {
                $scope.detailInfo.headerInfo.warehouseCode = "";
                $scope.detailInfo.headerInfo.inArea = "";
                $scope.detailInfo.headerInfo.sapInArea = "";
            };

            $scope.selectArea = function () {
                if (!$scope.detailInfo.headerInfo.warehouseCode) {
                    ybWidget.ShowToaster('error', '请选择调出仓库');
                    return;
                }
                var param = {};
                param.warehouseCode = $scope.detailInfo.headerInfo.warehouseCode;
                ybWidget.DepotAreaSelector(param).then(function (data) {
                    $scope.detailInfo.headerInfo.inArea = data.areaCode;
                    $scope.detailInfo.headerInfo.sapInArea = data.sapCode;
                })
            };

            $scope.clearArea = function () {
                $scope.detailInfo.headerInfo.inArea = "";
                $scope.detailInfo.headerInfo.sapInArea = "";
            };

            $scope.editData = function (item) {
                getDetail(item.id).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $scope.tabFlag.type = 1;
                    $scope.detailInfo = data;

                    angular.forEach($scope.detailInfo.materialList, function (item) {
                        item.statusShow = $filter("ProgressStatus")(item.status, "outDepot");
                        item.lastAmount = item.amount;
                        item.canInQty = ybWidget.Plus(ybWidget.Minus(item.qty, item.sendQty), item.amount);
                    });
                    $scope.changeTab(2);
                })
            };

            $scope.checkQty = function (item) {
                item.deliveryQtyErr = false;
                if (item.amount === "") {
                    item.deliveryQtyErr = true;
                    item.deliveryQtyErrMsg = "出库数量不能为空";
                    return;
                }
                if(item.amount>99999.999){
                    item.amount=99999.999;
                }
                if (parseFloat(item.amount) > parseFloat(item.canInQty)) {
                    item.deliveryQtyErr = true;
                    item.deliveryQtyErrMsg = "出库数量不能大于可出库数量";
                }
            };

            $scope.mandatoryFinish = function (billNo, billId, materialsNo) {
                ybWidget.ShowMessage("确定要结单吗？", MESG_TYPE.Ask).then(function () {
                    var service = "device";
                    var reqUrl = "/backOutbound/manualFinish";
                    var param = {
                        billNo: billNo,
                        materialsNo: materialsNo ? materialsNo : null
                    };

                    ybHttp.Post(service, reqUrl, param).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        if (!!billId) {
                            getDetail(billId).then(function (data) {
                                if (!data) {
                                    return;
                                }
                                $scope.detailInfo = data;
                                angular.forEach($scope.detailInfo.materialList, function (item) {
                                    item.statusShow = $filter("ProgressStatus")(item.status, "outDepot");
                                });
                            });
                        }
                        getDataList();
                    });
                }, function () {
                });
            };

            $scope.saveData = function (isValid) {
                if (!isValid) {
                    $scope.submitted = true;
                    return;
                }
                var listValid = checkListValid();
                if (!listValid) {
                    return;
                }
                var service = "device";
                var reqUrl = "/backOutbound/add";

                if ($scope.tabFlag.type === 1) {
                    reqUrl = "/backOutbound/update";
                }
                var param = angular.copy($scope.detailInfo);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    if ($scope.tabFlag.type === 0) {
                        getDataList(1);
                    } else {
                        getDataList();
                    }
                    $scope.changeTab(0);
                });
            };

            $scope.prevStep = function () {
                $scope.steps.curStep = 0;
            };

            $scope.inbound = function (item) {
                getDetail(item.id).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $scope.detailInfo = data;
                    angular.forEach($scope.detailInfo.materialList, function (item) {
                        item.statusShow = $filter("ProgressStatus")(item.status, "outDepot");
                    });
                    $scope.changeTab(4);
                });
            };

            $scope.selDepot = function (item) {
                var param = {
                    warehouseCode: $scope.detailInfo.headerInfo.warehouseCode
                };
                ybWidget.DepotSelector(param).then(function (data) {
                    item.warehouseCode = data.warehouseCode;
                    item.areaCode = data.areaCode;
                    item.locationCode = data.locationCode;
                })
            };

            $scope.clearDepot = function (item) {
                item.warehouseCode = "";
                item.areaCode = "";
                item.locationCode = "";
            };

            $scope.inboundSubmit = function () {
                var service = "device";
                var reqUrl = "/warehouseDeliveryRecord/receive";
                var param = {
                    headerInfo: {},
                    detailList: []
                };
                var errorCount = 0;
                param.headerInfo.businessOrder = $scope.detailInfo.headerInfo.inboundOrder;
                angular.forEach($scope.detailInfo.materialList, function (item) {
                    var listRow = {};
                    listRow.materialNo = item.materialsNo;
                    listRow.warehouseCode = item.warehouseCode;
                    listRow.areaCode = item.areaCode;
                    listRow.locationCode = item.locationCode;
                    item.locationErr = false;
                    if((item.status === 0 || item.status === 1) && !item.locationCode){
                        item.locationErr = true;
                        errorCount++;
                    }
                    param.detailList.push(listRow);
                });
                if(errorCount> 0){
                    return;
                }
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0);
                    getDataList();
                });
            };

            $scope.cancel = function () {
                $scope.changeTab(0);
            };

            $scope.sapPost = function (item) {
                var service = "device";
                var reqUrl = "/backOutbound/sapPost";
                var param = {};
                param.id = item.id;
                ybWidget.ShowMessage("确定要采购凭证收货吗？", MESG_TYPE.Ask).then(function () {
                    ybHttp.Post(service, reqUrl, param).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    });
                });
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            function initData() {
                $scope.detailInfo = {
                    headerInfo: {},
                    materialList: []
                };
                $scope.toolbarConfig.showMateriel = false;
                $scope.steps.curStep = 0;
                $scope.tabFlag = {};
            }

            function getDataList(curPage) {
                $scope.markInfo = {
                    allChecked: false,
                    curRow: -1
                };
                $scope.exportParam.sectionParam.ids = [];
                var service = "device";
                var reqUrl = "/backOutbound/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        item.createDateShow = $filter("YbDate")(item.createDate,"yyyy-MM-dd HH:mm:ss");
                        item.statusShow = $filter("ProgressStatus")(item.status, "outDepot");
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            function viewSapError(item) {
                ybWidget.ShowMessage(item.sapErrorMsg, MESG_TYPE.Note);
            }

            function geBillIds() {
                $scope.exportParam.sectionParam.ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.ids.push(item.id);
                    }
                });
            }

            function checkAll() {
                var selectAll = false;
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    if (!$scope.dataList[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            function getWarehouse() {
                var service = "device";
                var reqUrl = "/warehouse/listWarehouses";
                var param = {pageNum: 0, pageSize: 0};
                ybHttp.Get(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return false;
                    }
                    $scope.warehouseList = resp.value.list;
                })
            }
            function getPurchaseBill(curPage) {
                var service = "device";
                var reqUrl = "/buyBack/list";
                var param = {};
                param.pageSize = $scope.billPageInfo.pageSize;
                if (!!curPage) {
                    $scope.billPageInfo.curPage = curPage;
                }
                param.pageNum = $scope.billPageInfo.curPage;
                param.filters = {};
                param.filters.backOrder = $scope.orderCondition.backOrder;
                param.filters.status = 4;
                param.filters.invalid = 0;
                param.filters.outboundEnable = 1;
                param.filters.seller = false;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.createDateShow = $filter("YbDate")(item.createDate);
                        item.createDate = $filter("YbDate")(item.createDate, 'yyyy-MM-dd HH:mm:ss');
                        item.createOutStatusDes = $filter("ProgressStatus")(item.createOutStatus, "returnApply");
                        item.outStatusDes = $filter("ProgressStatus")(item.outStatus, "outDepot");
                        item.statusDes = $filter("ProgressStatus")(item.status, "auditState");
                    });
                    $scope.billPageInfo.totalRows = resp.value.total;
                    $scope.billPageInfo.totalPages = resp.value.pages;
                    $scope.purchaseBillList = resp.value.list;
                });
            }

            function getDetail(billId) {
                var service = "device";
                var reqUrl = "/backOutbound/item";
                var param = {};
                param.id = billId;
                return ybHttp.Get(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return false;
                    }
                    resp.value.headerInfo.sapStatusShow = $filter("ProgressStatus")(resp.value.headerInfo.sapStatus, "sap");
                    return resp.value;
                });
            }

            function viewDetail(item) {
                getDetail(item.id).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $scope.detailInfo = data;

                    angular.forEach($scope.detailInfo.materialList, function (item) {
                        item.statusShow = $filter("ProgressStatus")(item.status, "outDepot");
                    });
                    $scope.changeTab(3);
                })
            }

            function checkListValid() {
                var isValid = true;
                for (var i = 0, len = $scope.detailInfo.materialList.length; i < len; i++) {
                    if ($scope.detailInfo.materialList[i].amount === "") {
                        isValid = false;
                        $scope.detailInfo.materialList[i].deliveryQtyErr = true;
                        $scope.detailInfo.materialList[i].deliveryQtyErrMsg = "出库数量不能为空";
                        break;
                    }
                    if (parseInt($scope.detailInfo.materialList[i].amount) > parseInt($scope.detailInfo.materialList[i].canInQty)) {
                        isValid = false;
                        $scope.detailInfo.materialList[i].deliveryQtyErr = true;
                        $scope.detailInfo.materialList[i].deliveryQtyErrMsg = "出库数量不能大于可出库数量";
                        break;
                    }
                }
                return isValid;
            }

            (function () {
                getDataList();
                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);
